package cn.elwy.rcp.ui.listener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
 * 移动Shell监听
 * 
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class MoveShellListener extends MouseAdapter implements MouseMoveListener {
  private Point startPoint;

  public void mouseDown(MouseEvent e) {
    if (e.button == 1) {
      startPoint = new Point(e.x, e.y);
    }
  }

  public void mouseUp(MouseEvent e) {
    startPoint = null;
  }

  @Override
  public void mouseMove(MouseEvent e) {
    Control control = (Control) e.widget;
    if (startPoint == null) {
      return;
    }
    if ((e.stateMask & SWT.BUTTON1) != 0) {
      Shell shell = control.getShell();
      if (shell != null) {
        Point p = shell.toDisplay(e.x, e.y);
        p.x -= startPoint.x;
        p.y -= startPoint.y;
        shell.setLocation(p);
      }
    }
  }

}
